package com.example.seata_order.controller;


import com.example.seata_order.dao.OrderDao;
import com.example.seata_order.pojo.Order;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
public class orderController {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    public RestTemplate restTemplate;

    @GetMapping("/addOrder")
    @GlobalTransactional
//    @Transactional(rollbackFor = Exception.class)
    public String addOrder(){

        Order o = new Order();
        o.setProductId(12345);
        o.setTotalAmount(100);
        o.setStatus(1);


        int rst = orderDao.addOrder(o);
        System.out.println("rst : " + rst);

        String msg = restTemplate.getForObject("http://localhost:8002/addStock" , String.class);

        System.out.println("msg : " + msg);

        return msg;
//        docker run --env MODE=standalone -v C:/Users/P0186595/docker/nacos/conf/:/home/nacos/conf/ -v C:/Users/P0186595/docker/nacos/logs/:/home/nacos/logs/ --name nacos -d -p 8848:8848 nacos/nacos-server

    }
    @GlobalTransactional
    @GetMapping("/canalOrder/{id}")
    public String canalOrder(@PathVariable Integer id){
        int canalOrder = orderDao.canalOrder(id);
        System.out.print(canalOrder);
        return canalOrder==1?"取消成功":"取消失败";

    }

}
